<?php
# Sample that explains you how to use the PHP RawsClient lib to communicate with the RAMS 'concurrent' resource
#
# Copyright (C) 2010 rambla.be

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require_once '../RawsClient/Raws/Rams.php';

# Provide your own credentials here
define('BASE', 'http://rams.mon01.rambla.be/');
define('USER', 'XXX');
define('PWD', 'XXX');

try {
  # Initialize the HTTP connection object
  $rams = new Rams(USER, PWD, BASE);

  echo "->Retrieving a RAMS Concurrent Feed:\n";
  # Pass a Query-String to get all concurrent entries recorded on March 21 2010 of type "rtmp-live" (40 entries per page)
  $query = new Rams_ConcurrentQuery();
  $query->setFrom("2010-10-3-21-00");
  $query->setUntil("2010-10-3-22-00");
  $query->setType("rtmp-live");
  $query->setPaginateBy("50");
  # get the first page feed
  $feed = $rams->getConcurrentV1Feed($query);
  while($feed) # start a while loop, in case there are more than 50 results => multiple pages
  { 
    foreach ($feed as $entry) {
      # Retrieve the entry element's "type" attribute
      echo "Found " . $entry->type . " entry \n";
      # Retrieve the value of the params child elements
      echo "Path: " . $entry->content->params->path->text . "\n";
      echo "Hits: " . $entry->content->params->hits->text . "\n";
      echo "Timestamp: " . $entry->content->params->timestamp->text . "\n";
      echo "Datetime: " . $entry->content->params->datetime->text . "\n";
      echo "\n";
    }
    # Check if RAMS has any more results left
    echo "URL at which next page can be retrieved : " $rams->getNextLink($feed) . "\n";
    # Check how many pages are available
    echo "Number of the last page can be retrieved : " $rams->getLastNr($feed) . "\n";
    echo "URL at which last page can be retrieved : " $rams->getLastLink($feed) . "\n";
    # get next feed, by sending a new request to the next link inside this page
    $feed = $rams->getNextFeed($feed);
  }

}
catch(Zend_Gdata_App_Exception $e) {
    # Report the exception to the user
    echo "\nCaught exception: " . get_class($e) . "\n";
    echo "Message: " . $e->getMessage() . "\n";
    # get the HTTP status code
    echo "HTTP Status Code: " . $e->getResponse()->getStatus() . "\n";
    echo "Response Body with exception details: " . $e->getResponse()->getBody() . "\n";
    # get the raws:error elements
    $rawsResponse = Raws::parseRawsResponse($e->getResponse());
    echo "Raws Code: " . $rawsResponse->getCode() . "\n";
    echo "Raws Message: " . $rawsResponse->getMsg() . "\n";
    $reasons = $rawsResponse->getReasons();
    foreach ($reasons as $reason) {
      echo "Raws Reason: " . $reason . "\n";
    }
    $details = $rawsResponse->getDetails();
    foreach ($details as $key => $value) {
      echo "Raws Detail: " . $key . " -> " . $value . "\n";
    }
}
catch (Zend_Exception $e) {
    echo "Caught exception: " . get_class($e) . "\n";
    echo "Message: " . $e->getMessage() . "\n";
}
